package main

import (
	"go_practice/structs"
)

type TreeNode = structs.TreeNode

// 中序遍历序列，然后扫描统计
func findMode(root *TreeNode) []int {
	var base, count, maxCount int
	var ans []int
	update := func(x int) {
		if x == base {
			count++
		} else {
			base = x
			count = 1
		}
		if count == maxCount {
			ans = append(ans, x)
		} else if count > maxCount {
			ans = []int{base}
			maxCount = count
		}
	}

	var dfs func(node *TreeNode)
	dfs = func(node *TreeNode) {
		if node == nil {
			return
		}
		dfs(node.Left)
		//中序     中序序列中，相同的数字是扎推出现的，序列是有序的
		update(node.Val)
		dfs(node.Right)
	}
	dfs(root)
	return ans
}
